package com.comtime.manager;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;

import com.comtime.entity.UserInfo;
import com.comtime.swdevice.Conversion;
import com.comtime.swdevice.SWDevice;
import com.comtime.utils.LogUtils;
import com.comtime.utils.MusicPlayer;
import com.comtime.utils.MyConfig;
import com.comtime.utils.Util;

public class SwDeviceNew extends SWDevice {

	// int deviceStatus=0;//0:null 5:disconnect 2:connect

	public MusicPlayer musicPlayer;

	public int battery = 0;

	public boolean isChecking = false;
	
	public ArrayList<UserInfo> userInfos=new ArrayList<UserInfo>();//账户列表
	
	public int userLength=0;//账户列表个数
	
	public boolean checkUserOk=false;//验证设备里有没有该用户

	
	public String versionName="";
	
	public int versionCode;
	
	public  char versionChar;
	
	public boolean isOADing;

	public boolean needOAD;
	
	
	public SwDeviceNew(Context context, int tag) {
		super(context, tag);
		// TODO Auto-generated constructor stub
	}

	 @Override
	public void reset() {
		// TODO Auto-generated method stub
		super.reset();
		versionName = "";
		versionCode = 0;

	}

	//
	// public int getDeviceStatus() {
	// return deviceStatus;
	// }
	//
	// public void setDeviceStatus(int deviceStatus) {
	// this.deviceStatus = deviceStatus;
	// }
	// @Override
	// public boolean isConnected() {
	// if (deviceStatus >= 2 && deviceStatus < 5) {
	// return true;
	// }
	// return false;
	// }
	@Override
	public void connectGatt() {
		// TODO Auto-generated method stub
		if (MyConfig.bluetoothEnabled) {
			super.connectGatt();
		}
	}

	@Override
	public boolean justConnect() {
		// TODO Auto-generated method stub
		if (MyConfig.bluetoothEnabled) {
			return super.justConnect();
		}
		return false;
	}

	@Override
	public void disconnect() {
		// TODO Auto-generated method stub

		super.disconnect();
		checkUserOk=false;
	}

	@Override
	public void remove() {
		// TODO Auto-generated method stub
		super.remove();
		checkUserOk=false;
	}

	@Override
	public void sendKeyValue(byte[] values) {
		// TODO Auto-generated method stub
		if (MyConfig.bluetoothEnabled) {
			super.sendKeyValue(values);
		}
	}

	/**
	 * 校验用户手机号
	 */
	public void checkUserPhone(String phonenum) {

		byte[] phoneByte = phonenum.getBytes();
		// byte[] phoneByte=Util.stringToBytes(phonenum);
		int length = phoneByte.length + 3;
		byte[] values = new byte[length];
		values[0] = 0x07;
		values[1] = 0x04;
		values[2] = (byte) length;

		for (int i = 3, j = 0; i < length; i++, j++) {
			values[i] = phoneByte[j];
		}

		sendToDevice(values);
	}

	/**
	 * 添加子用户
	 */
	public void sendAddUserPhone(String phonenum) {

		byte[] phoneByte = phonenum.getBytes();
		int length = phoneByte.length + 3;
		byte[] values = new byte[length];
		values[0] = 0x07;
		values[1] = 0x05;
		values[2] = (byte) length;

		for (int i = 3, j = 0; i < length; i++, j++) {
			values[i] = phoneByte[j];
		}

		sendToDevice(values);
	}

	/**
	 * 删除子用户
	 */
	public void sendDeleteUserPhone(String phonenum) {

		byte[] phoneByte = phonenum.getBytes();
		int length = phoneByte.length + 3;
		byte[] values = new byte[length];
		values[0] = 0x07;
		values[1] = 0x06;
		values[2] = (byte) length;

		for (int i = 3, j = 0; i < length; i++, j++) {
			values[i] = phoneByte[j];
		}

		sendToDevice(values);
	}
	
	/**
	 * 查询普通用户列表命令
	 */
	public void sendCheckOutAllUser() {
		byte[] values = new byte[3];
		values[0] = 0x07;
		values[1] = 0x07;
		values[2] = 3;
		sendToDevice(values);

	}
	/**
	 * 手机向设备写管理员身份验证命令
	 */
	public void sendCheckAdministrator(String pwd) {
		byte[] phoneByte = pwd.getBytes();
		int length = phoneByte.length + 3;
		byte[] values = new byte[length];
		values[0] = 0x07;
		values[1] = 0x12;
		values[2] = (byte) length;

		for (int i = 3, j = 0; i < length; i++, j++) {
			values[i] = phoneByte[j];
		}
		sendToDevice(values);
	}

	/**
	 * 管理员密码更改命令
	 */
	public void sendChangeAdministratorPwd(String oldPwd,String newPwd) {
		String pwd=oldPwd+newPwd;
		byte[] phoneByte = pwd.getBytes();
		int length = phoneByte.length + 3;
		byte[] values = new byte[length];
		values[0] = 0x07;
		values[1] = 0x13;
		values[2] = (byte) length;

		for (int i = 3, j = 0; i < length; i++, j++) {
			values[i] = phoneByte[j];
		}
		sendToDevice(values);
	}
	
	
	/**
	 * 指纹模块开锁命令
	 */
	public void sendUnlock( int keyId) {

		byte[] values = new byte[] { 0x07, 0x14, 0x08,
				Conversion.hiUint16(keyId), Conversion.loUint16(keyId), 0x17,
				0x08, 0x0C };

		// byte[] values = new byte[]{0x07,0x14,0x08,0x17,0x08,0x10,0x18,0x0C};

		values[7] = (byte) (values[0] ^ values[1] ^ values[2] ^ values[3]
				^ values[4] ^ values[5] ^ values[6]);

		LogUtils.e("woca", " sendUnlock:" + Conversion.Bytes2HexString(values));
		sendToDevice(values);

	}
	
	/**
	 * 指纹模块开锁记录查询命令
	 */
	public void sendUnlockRecordQuery( ) {
		 
		byte[] values = new byte[]{0x07,0x15};
		 
		sendToDevice(values);
	}
	
	
	/**
	 * 指纹模块注册指纹命令
	 */
	public void sendRegisterlock() {
		 
		byte[] values = new byte[]{0x07,0x16};
		 
		sendToDevice(values);
	}
	
	
	/**
	 * 指纹模块锁状态查询命令
	 */
	public void sendLockStateQuery() {
		 
		byte[] values = new byte[]{0x07,0x17};
		 
		sendToDevice(values);
	}

	public void sendLockTimeMode(int mode) {
		byte[] values = new byte[] { 0x07, 0x0A, 0x04, 0x00 };
		switch (mode) {
		case 0:
			values[3]=0x01;
			break;
		case 1:
			values[3]=0x03;
			break;
		case 2:
			values[3]=0x06;
			break;
		default:
			break;
		}
		sendToDevice(values);
	}

}
